package com.ruoyi.web.controller.project;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.domain.entity.*;
import com.ruoyi.project.domain.query.ProjectFeeQuery;
import com.ruoyi.project.service.*;
import com.ruoyi.system.service.ISysDictDataService;
import io.swagger.annotations.ApiOperation;
import org.apache.xmlbeans.impl.xb.xsdschema.All;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/project/allocation")
public class ProjectAllocationController {

    @Resource
    private IProjectFeeService projectFeeService;

    @Resource
    private IProjectAllocationService projectAllocationService;

    @Resource
    private IProjectAllocationDetailService detailService;

    @Resource
    private IProjectAccountingService accountingService;

    @Resource
    private ISysDictDataService dictDataService;

    @Resource
    private IProjectParticipanService joinPeopleService;

    @Resource
    private IProjectPhaseService phaseService;

    @ApiOperation("生成绩效信息")
    @PostMapping(value = "/create")
    public R createAlloclation(@RequestBody ProjectAllocation projectFee) {
        if (StringUtils.isEmpty(projectFee.getProjectNo())){
            throw new RuntimeException("后端接口异常");
        }
        projectAllocationService.addAllocation(projectFee.getProjectNo());
        List<ProjectAllocation> allocationList = projectAllocationService.getAllocationList(projectFee.getProjectNo(), projectFee.getCurrent());

        List<AllocationDetail> resultList = new ArrayList<>();
        // 过滤操作
        List<ProjectAllocation> filteredList = allocationList.stream()
                .filter(allocation -> allocation.getDetailList() != null && !allocation.getDetailList().isEmpty())
                .collect(Collectors.toList());

        for (ProjectAllocation allocation : filteredList) {
            List<AllocationDetail> detailList = allocation.getDetailList();
            for (AllocationDetail detail : detailList) {
                detail.setPostName(allocation.getPostName());
                detail.setParentMoney(allocation.getMoney());
                detail.setParentRatio(allocation.getRatio());
                detail.setParentRemark(allocation.getRemark());
                detail.setPostId(allocation.getPostId());
                detail.setOper(allocation.isOper());
                detail.setWarrantyMoney(allocation.getWarrantyMoney());
                resultList.add(detail);
            }
        }

        Map<String,List<AllocationDetail>> map = new HashMap<>();
        map.put("allocationList",resultList);
        return R.ok(map);
    }


    @ApiOperation("重新计算生成绩效信息")
    @PostMapping(value = "/reCreate")
    public R reCreate(@RequestBody ProjectAllocation projectFee) {

        //如果是验收中进来的 验收还没有项目提成 直接返回
        String projectNo = projectFee.getProjectNo();
        if (StringUtils.isEmpty(projectFee.getProjectNo())){
            throw new RuntimeException("后端接口异常!");
        }
        LambdaQueryWrapper<ProjectPhase> phaseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        phaseLambdaQueryWrapper.eq(ProjectPhase::getProjectNo, projectNo);
        phaseLambdaQueryWrapper.eq(ProjectPhase::getPhaseId,"5");//如果有了核算信息
        List<ProjectPhase> phaseList = phaseService.list(phaseLambdaQueryWrapper);
        if (!projectNo.contains("XS")){//排除掉销售项目
            if (CollectionUtils.isEmpty(phaseList)) {
                return R.ok();
            }
        }


        //需要重新计算更新父项的信息和子项的信息
        if (StringUtils.isEmpty(projectFee.getProjectNo())){
            throw new RuntimeException("后端接口异常");
        }

        //先获取项目提成
        Map<String, BigDecimal> mapR = accountingService.getAllList(projectFee.getProjectNo());
        // 分为两部分 扣除质保金部分和质保金部分
        BigDecimal allCommission = BigDecimal.ZERO;
        if ("1".equals(projectFee.getCurrent())) {
            //扣除质保金部分
            allCommission = mapR.get("allCommission");
        }else {
            //纯质保金部分
            allCommission = mapR.get("allCommissionWarranty");
        }

        LambdaQueryWrapper<ProjectAllocation> queryWrapperAllocation = new LambdaQueryWrapper<>();
        queryWrapperAllocation.eq(ProjectAllocation::getProjectNo, projectFee.getProjectNo());
        List<ProjectAllocation> allocationListUpdate = projectAllocationService.list(queryWrapperAllocation);
        for (ProjectAllocation allocation : allocationListUpdate) {
            Long id = allocation.getId();
            LambdaQueryWrapper<AllocationDetail> queryWrapperDetail = new LambdaQueryWrapper<>();
            queryWrapperDetail.eq(AllocationDetail::getProjectNo, projectFee.getProjectNo());
            queryWrapperDetail.eq(AllocationDetail::getAllocationId, id);
            queryWrapperDetail.eq(AllocationDetail::getCurrent, projectFee.getCurrent());

            BigDecimal allocationMoneys = allCommission
                    .multiply(allocation.getRatio())       // allCommission * ratio
                    .divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
            allocation.setMoney(allocationMoneys);
            if ("2".equals(projectFee.getCurrent())) {
                allocation.setWarrantyMoney(allocationMoneys);
            }
            projectAllocationService.updateById(allocation);
            //设置完标准岗位绩效金额后 跳过销售负责人  因为销售负责人的绩效金额是项目提成减去其他人绩效金额之和的
            if (allocation.getPostId().equals(5L)){
                continue;
            }
            List<AllocationDetail> detailListUpdate = detailService.list(queryWrapperDetail);
            for (AllocationDetail detail : detailListUpdate) {
                //设置标准岗位绩效金额 allCommission * ratio / 100 / 总人数
                BigDecimal personCount = BigDecimal.valueOf(detailListUpdate.size()).max(BigDecimal.ONE); // 防止除以0
                //标准岗位绩效金额 除以同一岗位人数 得到该参与人员的标准岗位绩效金额
                BigDecimal allocationMoney = BigDecimal.ZERO;
                if ("1".equals(projectFee.getCurrent())) {
                     allocationMoney = (allocation.getMoney() != null) ? allocation.getMoney() : BigDecimal.ZERO;
                }else{
                     allocationMoney = (allocation.getWarrantyMoney() != null) ? allocation.getWarrantyMoney() : BigDecimal.ZERO;
                }

                BigDecimal resultMoneyCur = allocationMoney.divide(personCount, 2, RoundingMode.HALF_UP);

                //总的绩效
                BigDecimal performanceBonus = resultMoneyCur
                        .multiply(detail.getRatio())       // allCommission * ratio
                        .divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
                System.out.println("绩效金额: " + performanceBonus);
                detail.setMoney(performanceBonus);

                //设置个人绩效
                String personAllocation = dictDataService.selectDictLabel("allocation_type", "1");
                BigDecimal personAllocationMoney = performanceBonus.multiply(new BigDecimal(personAllocation));
                detail.setPersonMoney(personAllocationMoney);

                //设置部门绩效
                String deptAllocation = dictDataService.selectDictLabel("allocation_type", "2");
                BigDecimal deptAllocationMoney = performanceBonus.multiply(new BigDecimal(deptAllocation));
                detail.setDeptMoney(deptAllocationMoney);


                //使用标准岗位绩效金额乘以绩效系数除以100得到该参与人员的绩效金额
//               BigDecimal resultMoney = performanceBonus.multiply(detail.getRatio()).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
                detailService.updateById(detail);
            }
        }
        detailService.setSaleMoney(projectFee.getProjectNo(),projectFee.getCurrent());
        List<ProjectAllocation> allocationList = projectAllocationService.getAllocationList(projectFee.getProjectNo(), projectFee.getCurrent());

        List<AllocationDetail> resultList = new ArrayList<>();
        // 过滤操作
        List<ProjectAllocation> filteredList = allocationList.stream()
                .filter(allocation -> allocation.getDetailList() != null && !allocation.getDetailList().isEmpty())
                .collect(Collectors.toList());

        for (ProjectAllocation allocation : filteredList) {
            List<AllocationDetail> detailList = allocation.getDetailList();
            for (AllocationDetail detail : detailList) {
                detail.setPostName(allocation.getPostName());
                detail.setParentMoney(allocation.getMoney());
                detail.setParentRatio(allocation.getRatio());
                detail.setParentRemark(allocation.getRemark());
                detail.setPostId(allocation.getPostId());
                detail.setOper(allocation.isOper());
                detail.setWarrantyMoney(allocation.getWarrantyMoney());
                resultList.add(detail);
            }
        }

        Map<String,List<AllocationDetail>> map = new HashMap<>();
        map.put("allocationList",resultList);
        return R.ok(map);
    }


    @ApiOperation("重新计算生成绩效信息 质保金部分")
    @PostMapping(value = "/reCreateWarranty")
    public R reCreateWarranty(@RequestBody ProjectAllocation projectFee) {
        //需要重新计算更新父项的信息和子项的信息
        if (StringUtils.isEmpty(projectFee.getProjectNo())){
            throw new RuntimeException("后端接口异常");
        }


        //先获取项目提成
        Map<String, BigDecimal> mapR = accountingService.getAllList(projectFee.getProjectNo());
        // 分为两部分 扣除质保金部分和质保金部分
        BigDecimal allCommission = BigDecimal.ZERO;
        allCommission = mapR.get("allCommissionWarranty");
        LambdaQueryWrapper<ProjectAllocation> queryWrapperAllocation = new LambdaQueryWrapper<>();
        queryWrapperAllocation.eq(ProjectAllocation::getProjectNo, projectFee.getProjectNo());
        List<ProjectAllocation> allocationListUpdate = projectAllocationService.list(queryWrapperAllocation);
        for (ProjectAllocation allocation : allocationListUpdate) {
            Long id = allocation.getId();
            LambdaQueryWrapper<AllocationDetail> queryWrapperDetail = new LambdaQueryWrapper<>();
            queryWrapperDetail.eq(AllocationDetail::getProjectNo, projectFee.getProjectNo());
            queryWrapperDetail.eq(AllocationDetail::getAllocationId, id);
            queryWrapperDetail.eq(AllocationDetail::getCurrent, projectFee.getCurrent());
            BigDecimal allocationMoneys = allCommission
                    .multiply(allocation.getRatio())       // allCommission * ratio
                    .divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
            allocation.setWarrantyMoney(allocationMoneys);
            projectAllocationService.updateById(allocation);
            //设置完标准岗位绩效金额后 跳过销售负责人  因为销售负责人的绩效金额是项目提成减去其他人绩效金额之和的
            if (allocation.getPostId().equals(5L)){
                continue;
            }
            List<AllocationDetail> detailListUpdate = detailService.list(queryWrapperDetail);
            for (AllocationDetail detail : detailListUpdate) {
                //设置标准岗位绩效金额 allCommission * ratio / 100 / 总人数
                BigDecimal personCount = BigDecimal.valueOf(detailListUpdate.size()).max(BigDecimal.ONE); // 防止除以0
                //标准岗位绩效金额 除以同一岗位人数 得到该参与人员的标准岗位绩效金额
                BigDecimal allocationMoney = BigDecimal.ZERO;
                allocationMoney = (allocation.getWarrantyMoney() != null) ? allocation.getWarrantyMoney() : BigDecimal.ZERO;
                BigDecimal resultMoneyCur = allocationMoney.divide(personCount, 2, RoundingMode.HALF_UP);

                BigDecimal performanceBonus = resultMoneyCur
                        .multiply(detail.getRatio())       // allCommission * ratio
                        .divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
                System.out.println("绩效金额: " + performanceBonus);
                detail.setEndDate(new Date());
                detail.setMoney(performanceBonus);


                //设置个人绩效
                String personAllocation = dictDataService.selectDictLabel("allocation_type", "1");
                BigDecimal personAllocationMoney = performanceBonus.multiply(new BigDecimal(personAllocation));
                detail.setPersonMoney(personAllocationMoney);

                //设置部门绩效
                String deptAllocation = dictDataService.selectDictLabel("allocation_type", "2");
                BigDecimal deptAllocationMoney = performanceBonus.multiply(new BigDecimal(deptAllocation));
                detail.setDeptMoney(deptAllocationMoney);

                //使用标准岗位绩效金额乘以绩效系数除以100得到该参与人员的绩效金额
//               BigDecimal resultMoney = performanceBonus.multiply(detail.getRatio()).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
                detailService.updateById(detail);
            }
        }

        //查询除了销售负责人的
        LambdaQueryWrapper<ProjectAllocation> allocationWrapperList = new LambdaQueryWrapper<ProjectAllocation>();
        allocationWrapperList.eq(ProjectAllocation::getProjectNo, projectFee.getProjectNo());
        allocationWrapperList.ne(ProjectAllocation::getPostId, 5L);

        List<ProjectAllocation> list = projectAllocationService.list(allocationWrapperList);
        //其他人已分配的
        BigDecimal already = BigDecimal.ZERO;
        for (ProjectAllocation allocation : list) {
            Long id = allocation.getId();
            LambdaQueryWrapper<AllocationDetail> queryWrapperDetail = new LambdaQueryWrapper<>();
            queryWrapperDetail.eq(AllocationDetail::getProjectNo, projectFee.getProjectNo());
            queryWrapperDetail.eq(AllocationDetail::getAllocationId, id);
            queryWrapperDetail.eq(AllocationDetail::getCurrent, "2");
            List<AllocationDetail> detailList = detailService.list(queryWrapperDetail);
            for (AllocationDetail detail : detailList) {
                already = already.add(detail.getMoney());
            }
        }
        BigDecimal resultSaleMoney = BigDecimal.ZERO;
        resultSaleMoney = allCommission.subtract(already);

        //过滤出销售负责人  postId等于5 的
        LambdaQueryWrapper<ProjectAllocation> saleWrapperList = new LambdaQueryWrapper<ProjectAllocation>();
        saleWrapperList.eq(ProjectAllocation::getProjectNo, projectFee.getProjectNo());
        saleWrapperList.eq(ProjectAllocation::getPostId, 5L);
        List<ProjectAllocation> saleList = projectAllocationService.list(saleWrapperList);

        for (ProjectAllocation sale : saleList) {
            LambdaQueryWrapper<AllocationDetail> saleDetail = new LambdaQueryWrapper<>();
            saleDetail.eq(AllocationDetail::getProjectNo, projectFee.getProjectNo());
            saleDetail.eq(AllocationDetail::getAllocationId, sale.getId());
            saleDetail.eq(AllocationDetail::getCurrent, "2");
            List<AllocationDetail> saleDetailList = detailService.list(saleDetail);
            for (AllocationDetail saleDetail2 : saleDetailList) {
                //销售负责人有多个的情况 平分
                int count = saleDetailList.size();
                BigDecimal result = resultSaleMoney.divide(new BigDecimal(count));

                //按比例
                BigDecimal resultMoney = result
                        .multiply(saleDetail2.getRatio())       // allCommission * ratio
                        .divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);

                saleDetail2.setMoney(resultMoney);



                //设置个人绩效
                String personAllocation = dictDataService.selectDictLabel("allocation_type", "1");
                BigDecimal personAllocationMoney = resultMoney.multiply(new BigDecimal(personAllocation));
                saleDetail2.setPersonMoney(personAllocationMoney);

                //设置部门绩效
                String deptAllocation = dictDataService.selectDictLabel("allocation_type", "2");
                BigDecimal deptAllocationMoney = resultMoney.multiply(new BigDecimal(deptAllocation));
                saleDetail2.setDeptMoney(deptAllocationMoney);



                detailService.updateById(saleDetail2);
            }
        }

//        detailService.setSaleMoney(projectFee.getProjectNo(),projectFee.getCurrent());



        List<ProjectAllocation> allocationList = projectAllocationService.getAllocationList(projectFee.getProjectNo(), projectFee.getCurrent());
        List<AllocationDetail> resultList = new ArrayList<>();
        // 过滤操作
        List<ProjectAllocation> filteredList = allocationList.stream()
                .filter(allocation -> allocation.getDetailList() != null && !allocation.getDetailList().isEmpty())
                .collect(Collectors.toList());

        for (ProjectAllocation allocation : filteredList) {
            List<AllocationDetail> detailList = allocation.getDetailList();
            for (AllocationDetail detail : detailList) {
                detail.setPostName(allocation.getPostName());
                detail.setParentMoney(allocation.getMoney());
                detail.setParentRatio(allocation.getRatio());
                detail.setParentRemark(allocation.getRemark());
                detail.setPostId(allocation.getPostId());
                detail.setOper(allocation.isOper());
                detail.setWarrantyMoney(allocation.getWarrantyMoney());
                resultList.add(detail);
            }
        }

        Map<String,List<AllocationDetail>> map = new HashMap<>();
        map.put("allocationList",resultList);
        return R.ok(map);
    }


    @ApiOperation("判断是否参与了这个项目,并且是参与分配的")
    @PostMapping(value = "/isJoin")
    public boolean isJoin(@RequestBody ProjectAllocation projectFee) {
        if (StringUtils.isEmpty(projectFee.getProjectNo())){
            throw new RuntimeException("后端接口异常");
        }
        Long userId = SecurityUtils.getUserId();

        //如果是总经理
        if (userId.equals(100L)){
            return true;
        }
        //有没有参与分配
        LambdaQueryWrapper<AllocationDetail> wrapper = new QueryWrapper<AllocationDetail>().lambda()
                .eq(AllocationDetail::getPersonId, userId)
                .eq(AllocationDetail::getProjectNo, projectFee.getProjectNo());

        List<AllocationDetail> personList = detailService.list(wrapper);
        if (CollectionUtils.isEmpty(personList)){
            return false;
        }
        return true;
    }




    @ApiOperation("获取列表")
    @PostMapping(value = "/list")
    public R getAllocationList(@RequestBody ProjectAllocation projectFee) {
        List<ProjectAllocation> allocationList = projectAllocationService.getAllocationList(projectFee.getProjectNo(), projectFee.getCurrent());
        // 过滤操作
        List<ProjectAllocation> filteredList = allocationList.stream()
                .filter(allocation -> allocation.getDetailList() != null && !allocation.getDetailList().isEmpty())
                .collect(Collectors.toList());
        List<AllocationDetail> resultList = new ArrayList<>();

        Long userId = SecurityUtils.getUserId();

        //判断是否有多个销售负责人
        Boolean moreSale = false;
        LambdaQueryWrapper<ProjectAllocation> wrapper = new QueryWrapper<ProjectAllocation>().lambda();
        wrapper.eq(ProjectAllocation::getProjectNo, projectFee.getProjectNo());
        wrapper.eq(ProjectAllocation::getCurrent, projectFee.getCurrent());
        wrapper.eq(ProjectAllocation::getPostId,5L);
        ProjectAllocation allocationOne = projectAllocationService.getOne(wrapper);

        if (allocationOne != null){
            Long id = allocationOne.getId();
            LambdaQueryWrapper<AllocationDetail> dewrapper = new QueryWrapper<AllocationDetail>().lambda();
            dewrapper.eq(AllocationDetail::getProjectNo, projectFee.getProjectNo());
            dewrapper.eq(AllocationDetail::getCurrent, projectFee.getCurrent());
            dewrapper.eq(AllocationDetail::getAllocationId,id);
            List<AllocationDetail> detailList = detailService.list(dewrapper);
            if (detailList != null && detailList.size() > 1){
                if (userId.equals(100L) || SecurityUtils.hasRole("gjadmin") || SecurityUtils.hasRole("admin")) {
                    //判断是否是核算环节
                    LambdaQueryWrapper<ProjectPhase> projectPhaseWrapper = new QueryWrapper<ProjectPhase>().lambda();
                    projectPhaseWrapper.eq(ProjectPhase::getProjectNo, projectFee.getProjectNo());
                    projectPhaseWrapper.eq(ProjectPhase::getPhaseId, 5);
                    List<ProjectPhase> phaseList = phaseService.list(projectPhaseWrapper);
                    if (CollectionUtils.isNotEmpty(phaseList) && phaseList.size() > 0){
                        moreSale = true;
                    }

                    if (projectFee.getProjectNo().contains("XS")){
                        moreSale = true;
                    }

                }
            }
        }



        for (ProjectAllocation allocation : filteredList) {
            List<AllocationDetail> detailList = allocation.getDetailList();
            for (AllocationDetail detail : detailList) {
                detail.setPostName(allocation.getPostName());
                detail.setParentMoney(allocation.getMoney());
                detail.setParentRatio(allocation.getRatio());
                detail.setParentRemark(allocation.getRemark());
                detail.setPostId(allocation.getPostId());
                detail.setOper(allocation.isOper());
                detail.setWarrantyMoney(allocation.getWarrantyMoney());

                detail.setMoreSale(moreSale);

                //如果不是总经理
                if (!userId.equals(100L)) {
                    if (detail.getAudit() !=null && detail.getAudit().size() > 0) {
                        //判断销售负责人或项目经理有没有确认过
                        if (detail.getAudit().contains(2) || detail.getAudit().contains(5) || detail.getAudit().contains(100)) {
                            detail.setRowStatus("1");//用于前端标记已经分配过了
                        }
                    }
                }else{
                    //是总经理
                    if (detail.getAudit() != null && detail.getAudit().size() > 0) {
                        //判断销售负责人或项目经理有没有确认过
                        if (detail.getAudit().contains(100)) {
                            detail.setRowStatus("1");//用于前端标记已经分配过了
                        }
                    }
                }
                resultList.add(detail);
            }
        }
        return R.ok(resultList);
    }


    @ApiOperation("获取列表 确认发放")
    @PostMapping(value = "/listAllocationConfirm")
    public R listAllocationConfirm(@RequestBody ProjectAllocation projectFee) {
        List<ProjectAllocation> allocationList = projectAllocationService.getAllocationList(projectFee.getProjectNo(), projectFee.getCurrent());
        // 过滤操作
        List<ProjectAllocation> filteredList = allocationList.stream()
                .filter(allocation -> allocation.getDetailList() != null && !allocation.getDetailList().isEmpty())
                .collect(Collectors.toList());
        List<AllocationDetail> resultList = new ArrayList<>();

        Long userId = SecurityUtils.getUserId();

        for (ProjectAllocation allocation : filteredList) {
            List<AllocationDetail> detailList = allocation.getDetailList();
            for (AllocationDetail detail : detailList) {
                detail.setPostName(allocation.getPostName());
                detail.setParentMoney(allocation.getMoney());
                detail.setParentRatio(allocation.getRatio());
                detail.setParentRemark(allocation.getRemark());
                detail.setPostId(allocation.getPostId());
                detail.setOper(allocation.isOper());
                detail.setWarrantyMoney(allocation.getWarrantyMoney());

                if (detail.getAlready() == null) {
                    detail.setWait(detail.getPersonMoney());
                }

                if (detail.getDeptAlready() == null) {
                    detail.setDeptWait(detail.getDeptMoney());
                }



                resultList.add(detail);
            }
        }
        return R.ok(resultList);
    }



    @ApiOperation("获取已分配可分配的金额信息")
    @PostMapping(value = "/getAllocationMoney")
    public R getAllocationMoney(@RequestBody ProjectAllocation projectFee) {
        Map<String, BigDecimal> allocationInfo = detailService.getAllocationInfo(projectFee.getProjectNo(), projectFee.getCurrent());
        return R.ok(allocationInfo);
    }


    @ApiOperation("获取当前操作人属于该项目的哪个角色 总经理 销售负责人  还是项目经理 还是其他")
    @PostMapping(value = "/getProjectRole")
    public R getProjectRole(@RequestBody ProjectAllocation projectFee) {
        Map<String, Boolean> map = new HashMap<>();
        Boolean boss = false;//总经理 吴总
        Boolean sale = false;//销售负责人
        Boolean manger = false;//项目经理
        Boolean other = false;//其他角色
        String projectNo = projectFee.getProjectNo();

        if (StringUtils.isEmpty(projectNo)) {
            throw new RuntimeException("后端接口出错,请联系管理员");
        }

        Long userId = SecurityUtils.getLoginUser().getUserId();
        if (userId.equals(100L)) {
            boss = true;//是总经理
        }

        //查询是否是销售负责人
        LambdaQueryWrapper<ProjectParticipan> joinPeopleWrapperSale = new LambdaQueryWrapper<>();
        joinPeopleWrapperSale.eq(ProjectParticipan::getProjectNo, projectNo);
        joinPeopleWrapperSale.eq(ProjectParticipan::getParticipantId, userId);
        joinPeopleWrapperSale.eq(ProjectParticipan::getPostId, 5L);//销售负责人的岗位id
        List<ProjectParticipan> saleList = joinPeopleService.list(joinPeopleWrapperSale);

        //如果能查询到 说明是销售负责人
        if (saleList != null && !saleList.isEmpty()) {
            sale = true;
        }


        //查询是否是项目经理
        LambdaQueryWrapper<ProjectParticipan> joinPeopleWrapperManger = new LambdaQueryWrapper<>();
        joinPeopleWrapperManger.eq(ProjectParticipan::getProjectNo, projectNo);
        joinPeopleWrapperManger.eq(ProjectParticipan::getParticipantId, userId);
        joinPeopleWrapperManger.eq(ProjectParticipan::getPostId, 2L);//项目经理的岗位id
        List<ProjectParticipan> mangerList = joinPeopleService.list(joinPeopleWrapperManger);

        //如果能查询到 说明是项目经理
        if (mangerList != null && !mangerList.isEmpty()) {
            manger = true;
        }

        map.put("boss",boss);
        map.put("sale",sale);
        map.put("manger",manger);
        map.put("other",other);
        return R.ok(map);
    }




    @ApiOperation("确认分配")
    @PostMapping(value = "/auditAllocation")
    public R auditAllocation(@RequestBody ProjectAllocation projectFee) {
        return R.ok(detailService.auditAllocation(projectFee.getProjectNo(), projectFee.getCurrent()));
    }

    @ApiOperation("查询还有哪些人未分配 除去销售负责人")
    @PostMapping(value = "/getAuditPermission")
    @RepeatSubmit
    public R getAuditPermission(@RequestBody ProjectAllocation projectFee) {
        return R.ok(detailService.getAuditPermission(projectFee.getProjectNo(), projectFee.getCurrent()));
    }



    @ApiOperation("分页")
    @GetMapping(value = "/page")
    public R<TableDataInfo> page(ProjectFeeQuery query) {
//        return R.ok(projectFeeService.getAllFee(query));
        return null;
    }


    @ApiOperation("新增")
    @PostMapping(value = "/save")
    public R savePhaseDictionary(@RequestBody ProjectAllocation projectFee) {
        return R.ok(projectAllocationService.save(projectFee));
    }


    @ApiOperation("修改")
    @PutMapping (value = "/update")
    public R updatePhaseDictionary(@RequestBody ProjectAllocation  projectFee) {
        return R.ok(projectAllocationService.updateById(projectFee));
    }


    @ApiOperation("删除")
    @DeleteMapping (value = "/{menuId}")
    public R deletePhaseDictionary(  @PathVariable("menuId") Integer id) {
        return R.ok(projectAllocationService.removeById(id));
    }


    @ApiOperation("详情")
    @GetMapping(value = "/details")
    public R getDetails(@RequestParam Integer id) {
        ProjectAllocation  projectFee = projectAllocationService.getById(id);
        return R.ok(projectFee);
    }

}
